package com.smbms.controller.recruitment;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.smbms.pojo.personnelfile.Education;
import com.smbms.pojo.recruitment.Ps_education;
import com.smbms.pojo.recruitment.Rt_Interview;
import com.smbms.pojo.recruitment.Rt_Resumestate;
import com.smbms.pojo.recruitment.Rt_resume;
import com.smbms.pojo.system.Og_Department;
import com.smbms.pojo.system.Og_Position;
import com.smbms.pojo.system.Og_User;
import com.smbms.service.commonality.CommonalityService;
import com.smbms.service.recruitmentservice.ResumeService.RmService;
import com.smbms.service.recruitmentservice.interview.InterviewService;
import com.smbms.service.recruitmentservice.releasejop.releasejopService;
import com.smbms.service.systemservice.positionService.positionService;
import com.smbms.service.systemservice.userService.UserService;

import com.smbms.util.FileUploadUtil;
import com.smbms.util.Tool;



@Controller
public class ResumeController {
	@Autowired
	private RmService rs;
	@Autowired
	private releasejopService rjs;
	@Autowired
	private InterviewService inService;
	@Autowired
	private positionService posService;
	@Autowired
	private UserService userService;

	// 简历管理页面
	@RequestMapping("/resume.html")
	public String GetResume(HttpServletRequest request) {
		List<Rt_Resumestate> rlist = rs.getRst();
		request.setAttribute("rlist", rlist);
		return "recruitment/rt_resume";
	}

	// 展示简历管理页面数据
	@RequestMapping(value = "/resume.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String GetRe() {
		// 
		Map<String, Object> map = new HashMap<String, Object>();
		//
		List<Rt_resume> rlist = rs.getResume();
		// 
		List<Rt_resume> alllisrt = new ArrayList<Rt_resume>();
		for (Rt_resume rs : rlist) {
			alllisrt.add(new Rt_resume(rs.getId(),rs.getName(), rs.getGraduate(), rs.getMajor(),
					rs.getWorkExperience(), rs.getRegistraTime(), rs.getRt_sex(), rs.getRt_education(), rs.getRt_post(),
					rs.getResumeState()));
		}
		map.put("data", alllisrt);
		System.out.println(JSONArray.toJSONString(map));
		return JSONArray.toJSONString(map);
	}

	// 简历状态
	@RequestMapping(value = "/rst.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String GetRst() {
		// 
		List<Rt_Resumestate> rl = rs.getRst();
		JSONArray jsonArray = new JSONArray();// 
		jsonArray.addAll(rl);// 
		String e = jsonArray.toJSONString();//
		System.out.println(e);
		return e;
	}

	// 简历管理模糊查询

	@RequestMapping(value = "/reumesss.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String asd(Rt_resume rtu) {
		Map<String, Object> map = new HashMap<String, Object>();
		Rt_resume rts = new Rt_resume(rtu.getName(),rtu.getWorkExperience(), 
				rtu.getRegistraTime(),rtu.getRt_post(),rtu.getResumeState());
		List<Rt_resume> rlist = rs.getResumeMH(rts);
		map.put("data", rlist);
		
		return JSONArray.toJSONString(map);
	}

		
	// 明细
	@RequestMapping(value = "rt_resumeselect.html/{id}")
	public String getResumeSelect(@PathVariable Integer id,HttpServletRequest request) {
		Rt_resume res = rs.getResumesel(id);
		List<Ps_education> etl = rs.getEducations();
		request.setAttribute("dtList", etl);//学历
		System.out.println(res.getAttachmen());
		request.setAttribute("res", res);
		return "recruitment/rt_resumeselect";
	}
	
	// 推荐二面、三面的明细
		@RequestMapping(value = "resumefiesig_select.html/{id}")
		public String getResumefilSelect(@PathVariable Integer id,HttpServletRequest request) {
			System.out.println("推荐二面");
			Rt_resume res = rs.getResumesel(id);
			request.setAttribute("res", res);
			//显示简历状态下拉框内容
			List<Rt_Resumestate> RList = rs.getRst();
			request.setAttribute("RList", RList);
			//学历
			List<Ps_education> etl = rs.getEducations();
			request.setAttribute("dtList", etl);//学历
			//显示面试表信息
			Rt_Interview result1 = rs.getViewRm(id);
			request.setAttribute("rm1", result1);
			return "recruitment/resumefiesig_select";
		}
	
	
	
	//删除
	@RequestMapping(value = "/delete.json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDeletes(Integer id) {
		boolean flag=true;
		HashMap<String, Object> map =new HashMap<String, Object>();
		int del =inService.delIn(id);
		if(del>0) {
			int result =rs.getDel(id);
			if(result>0) {
				map.put("data", flag);
			}
		}
		return JSONArray.toJSONString(map);
	}
	
		// 简历发布登记
			@RequestMapping(value = "/rt_resumeadd.html")
			public String getResumeadds(HttpServletRequest request) {
				List<Ps_education> etl = rs.getEducations();
				request.setAttribute("dtList", etl);//学历
				
				
			return "recruitment/rt_resumeadd";

			}
			//简历发布登记保存
			@RequestMapping(value = "/rt_resumeaddSave.html")
			public String getResumeaddSave(Rt_resume res,@RequestParam(required=false) MultipartFile a_attachmen,HttpServletRequest request)throws IllegalStateException, IOException  {
				
				if(res.getInterview().equals("1")) {
					res.setResumeState(2);
				}else {
					res.setResumeState(1);
				}
				
				if(a_attachmen.isEmpty()==false) {
					FileUploadUtil fuu =new FileUploadUtil(request, Tool.UPLOAD_PATH);
					String attachmen1 =fuu.all(a_attachmen, 5000000,".","docx","xlsx");
					res.setAttachmen(attachmen1);
				}
				
				int result =rs.getResumeAdd(res);
				if(result>0) {
					return "redirect:resume.html";
				}
				
				return "recruitment/rt_resumeadd";

			}
			
			//修改弹出框内容
			@RequestMapping(value = "/RecritmentNews.json", produces = "application/json;charset=utf-8")
			@ResponseBody
			public String getSelectModal(Rt_resume res) {
				System.out.println("修改");
				List<Rt_resume> rese = rs.getResumeModalSel(res);
				JSONArray jsonArray = new JSONArray();
				jsonArray.addAll(rese);
				String e = jsonArray.toJSONString();
				System.out.println(e);
				return e;
				
			}	
			//修改推荐面试意见
			@RequestMapping(value="/opinions.json",produces = "application/json;charset=utf-8")
			@ResponseBody
			public String getopinion(Rt_resume re) {
				System.out.println("修改推荐面试意见");
				int result =rs.getResumeModal(re);
				Map<String, Object> map =new HashMap<String,Object>();
				if(result>0) {
					map.put("data", "true");
				}else {
					map.put("data", "false");
				}
				return JSONArray.toJSONString(map);
			}
			
			//简历修改
			@RequestMapping(value="rt_resumeUpd.html/{id}")
			public String getResumeUpd(@PathVariable Integer id,HttpServletRequest request) {
				Rt_resume res = rs.getResumesel(id);
				request.setAttribute("res", res);
				List<Ps_education> etl = rs.getEducations();
				request.setAttribute("dtList", etl);//学历
				return "recruitment/rt_resumeUpd";
			}
			
			
			//简历修改保存
			@RequestMapping(value="rt_resumeUpdsave.html")
			public String getResumeSave(Rt_resume res,HttpServletRequest request) {
				
				res.setModifytime(new Date());
				int result=rs.getResumeUpd(res);
				if(result>0) {
					System.out.println("修改成功");
					return "redirect:resume.html";
				}
				return "recruitment/rt_resumeUpd";
			}
			

			@RequestMapping(value = "rt_position.html")
			public String getResulectjop(HttpServletRequest request) {
				System.out.println("hffh");
				List<Og_Department> depList = userService.getDepartmentList();
				request.setAttribute("depList", depList);
				return "recruitment/rt_position";
			}
			
			//弹出框职位列表
			@RequestMapping(value="/open.json",produces = "application/json;charset=utf-8")
			@ResponseBody
			public String getResumeOpen(Og_Position pos,HttpServletRequest request) {
				List<Og_Department> depList = userService.getDepartmentList();
				request.setAttribute("depList", depList);
				Map<String, Object> map =new HashMap<String,Object>();
				List<Og_Position> posList = posService.getPosition(pos);
				map.put("data", posList);
				return JSONArray.toJSONString(map);
			}
			
			//应聘职位添加
			//弹出框职位列表
			@RequestMapping(value="/AddEmployJob.json",produces = "application/json;charset=utf-8")
			@ResponseBody
			public String getAddEmployJob(Rt_resume res) {
				int result =rs.getAddEmployJobs(res);
				Map<String, Object> map =new HashMap<String,Object>();
				if(result>0) {
					map.put("data", "true");
				}else {
					map.put("data", "false");
				}
				return JSONArray.toJSONString(map);
			}
			
}
